using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemDrop : MonoBehaviour
{
    [SerializeField] private int maxDropCount;
    [SerializeField] private ItemData[] itemPool;
    [SerializeField] private List<ItemData> possibleDropItems;
    [SerializeField] private GameObject dropPrefab;



    public virtual void GenerateDrop()
    {
        if (itemPool.Length == 0)
            return;

        foreach (ItemData item in itemPool) 
        {
            if (Random.Range(1, 101) < item.dropChance)
            {
                possibleDropItems.Add(item);
            }
        }

        while(maxDropCount > 0)
        {
            if (possibleDropItems.Count == 0)
                break;
            int dropIndex = Random.Range(0, possibleDropItems.Count);
            DropItem(possibleDropItems[dropIndex]);
            maxDropCount--;
        }
        

    }



    protected void DropItem(ItemData _itemData)
    {
        GameObject itemObj = Instantiate(dropPrefab,transform.position,Quaternion.identity);
        Vector2 dropPos = new Vector2(Random.Range(-1, 1), Random.Range(10,15));
        itemObj.GetComponent<ItemObject>().SetItem(_itemData,dropPos);
    }
}
